<template>
  <el-dialog
    :title="!dataForm.uid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="活动id" prop="marketName">
      <el-input v-model="dataForm.marketName" placeholder="活动id"></el-input>
    </el-form-item>
    <el-form-item label="分组" prop="groupId">
      <el-input v-model="dataForm.groupId" placeholder="分组"></el-input>
    </el-form-item>
    <el-form-item label="任务名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="任务名称"></el-input>
    </el-form-item>
    <el-form-item label="任务描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="任务描述"></el-input>
    </el-form-item>
    <el-form-item label="任务规则" prop="rule">
      <el-input v-model="dataForm.rule" placeholder="任务规则"></el-input>
    </el-form-item>
    <el-form-item label="任务icon" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="任务icon"></el-input>
    </el-form-item>
    <el-form-item label="任务步骤" prop="stepConfig">
      <el-input v-model="dataForm.stepConfig" placeholder="任务步骤"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="开始时间"></el-input>
    </el-form-item>
    <el-form-item label="结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
    </el-form-item>
    <el-form-item label="按钮" prop="button">
      <el-input v-model="dataForm.button" placeholder="按钮"></el-input>
    </el-form-item>
    <el-form-item label="限制类型" prop="limitType">
      <el-input v-model="dataForm.limitType" placeholder="限制类型"></el-input>
    </el-form-item>
    <el-form-item label="限制类型cron" prop="limitCron">
      <el-input v-model="dataForm.limitCron" placeholder="限制类型cron"></el-input>
    </el-form-item>
    <el-form-item label="页面展示" prop="pageShow">
      <el-input v-model="dataForm.pageShow" placeholder="页面展示"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="排序字段，越大越靠前" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序字段，越大越靠前"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="是否发布(1:是，0:否)" prop="isPublish">
      <el-input v-model="dataForm.isPublish" placeholder="是否发布(1:是，0:否)"></el-input>
    </el-form-item>
    <el-form-item label="是否删除:0否 1是" prop="isDelete">
      <el-input v-model="dataForm.isDelete" placeholder="是否删除:0否 1是"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          uid: 0,
          marketName: '',
          groupId: '',
          name: '',
          description: '',
          rule: '',
          icon: '',
          stepConfig: '',
          startTime: '',
          endTime: '',
          button: '',
          limitType: '',
          limitCron: '',
          pageShow: '',
          createTime: '',
          updateTime: '',
          sort: '',
          status: '',
          isPublish: '',
          isDelete: ''
        },
        dataRule: {
          marketName: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ],
          groupId: [
            { required: true, message: '分组不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '任务名称不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '任务描述不能为空', trigger: 'blur' }
          ],
          rule: [
            { required: true, message: '任务规则不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '任务icon不能为空', trigger: 'blur' }
          ],
          stepConfig: [
            { required: true, message: '任务步骤不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ],
          button: [
            { required: true, message: '按钮不能为空', trigger: 'blur' }
          ],
          limitType: [
            { required: true, message: '限制类型不能为空', trigger: 'blur' }
          ],
          limitCron: [
            { required: true, message: '限制类型cron不能为空', trigger: 'blur' }
          ],
          pageShow: [
            { required: true, message: '页面展示不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序字段，越大越靠前不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          isPublish: [
            { required: true, message: '是否发布(1:是，0:否)不能为空', trigger: 'blur' }
          ],
          isDelete: [
            { required: true, message: '是否删除:0否 1是不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.uid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uid) {
            this.$http({
              url: this.$http.adornUrl(`/user/taskconfig/info/${this.dataForm.uid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.marketName = data.taskConfig.marketName
                this.dataForm.groupId = data.taskConfig.groupId
                this.dataForm.name = data.taskConfig.name
                this.dataForm.description = data.taskConfig.description
                this.dataForm.rule = data.taskConfig.rule
                this.dataForm.icon = data.taskConfig.icon
                this.dataForm.stepConfig = data.taskConfig.stepConfig
                this.dataForm.startTime = data.taskConfig.startTime
                this.dataForm.endTime = data.taskConfig.endTime
                this.dataForm.button = data.taskConfig.button
                this.dataForm.limitType = data.taskConfig.limitType
                this.dataForm.limitCron = data.taskConfig.limitCron
                this.dataForm.pageShow = data.taskConfig.pageShow
                this.dataForm.createTime = data.taskConfig.createTime
                this.dataForm.updateTime = data.taskConfig.updateTime
                this.dataForm.sort = data.taskConfig.sort
                this.dataForm.status = data.taskConfig.status
                this.dataForm.isPublish = data.taskConfig.isPublish
                this.dataForm.isDelete = data.taskConfig.isDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/taskconfig/${!this.dataForm.uid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uid': this.dataForm.uid || undefined,
                'marketName': this.dataForm.marketName,
                'groupId': this.dataForm.groupId,
                'name': this.dataForm.name,
                'description': this.dataForm.description,
                'rule': this.dataForm.rule,
                'icon': this.dataForm.icon,
                'stepConfig': this.dataForm.stepConfig,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'button': this.dataForm.button,
                'limitType': this.dataForm.limitType,
                'limitCron': this.dataForm.limitCron,
                'pageShow': this.dataForm.pageShow,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'sort': this.dataForm.sort,
                'status': this.dataForm.status,
                'isPublish': this.dataForm.isPublish,
                'isDelete': this.dataForm.isDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
